<template>
    <div class="add-role">
        <header class="add-header">
            <el-page-header :content="$route.query.id ? '编辑角色' : '添加角色'" @back="$router.back()" />
        </header>
        <main class="add-main">
            <div class="step-container">
                <el-steps direction="vertical" :active="activeStep">
                    <el-step title="角色信息" />
                    <el-step title="权限信息" />
                    <el-step title="检查并完成" />
                </el-steps>
            </div>
            <div v-show="activeStep === 1" class="form-container">
                <div class="title">角色信息</div>
                <div class="form">
                    <el-form class="form-box" ref="roleForm" :model="roleForm" :rules="roleRules" label-width="80px">
                        <el-form-item label="角色名称" prop="roleName">
                            <el-input v-model="roleForm.roleName" />
                        </el-form-item>
                        <el-form-item label="角色描述">
                            <el-input type="textarea" v-model="roleForm.remark" />
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div v-show="activeStep === 2" class="form-container">
                <div class="title">权限配置</div>
                <div class="tree-wrapper">
                    <div v-for="item in treeList" :key="item.id" class="tree-item">
                        <div class="tree-title">{{ item.title }}</div>
                        <el-tree ref="tree" :data="item.children" show-checkbox default-expand-all node-key="id"
                            highlight-current :props="{ label: 'title' }" />
                    </div>
                </div>
            </div>
            <div v-show="activeStep === 3" class="form-container">
                <div class="title">检查并完成</div>
                <div class="info">
                    <div class="form-item">
                        <div class="form-items1">角色名称：{{ roleForm.roleName }}</div>
                        <div class="form-items1">角色描述：{{ roleForm.remark }}</div>
                    </div>
                    <div class="form-items2">角色权限：</div>
                    <div class="tree-wrapper">
                        <div v-for="item in disabledTreeList" :key="item.id" class="tree-item">
                            <div class="tree-title">{{ item.title }}</div>
                            <el-tree ref="diabledTree" :data="item.children" show-checkbox default-expand-all node-key="id"
                                :highlight-current="false" :props="{ label: 'title' }" />
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <footer class="add-footer">
            <div class="btn-container">
                <el-button v-if="activeStep > 1" @click="decreseStep">上一步</el-button>
                <el-button v-if="activeStep < 3" type="primary" @click="increseStep">下一步</el-button>
                <el-button v-if="activeStep === 3" type="primary" @click="confirmAdd">确认</el-button>
            </div>
        </footer>
    </div>
</template>
<script>
import { getTreeListAPI, createRoleUserAPI, updateRoleAPI, getRoleDetailAPI } from '@/api/role'
export default {
    data() {
        return {
            activeStep: 1,
            roleForm: {
                roleName: '',
                remark: ''
            },
            roleRules: {
                roleName: [
                    { required: true, message: '请输入角色名称', trigger: 'blur' }
                ]
            },
            treeList: [], // 权限树形列表
            perms: [],
            activeName: 'tree',
            diabledTree: [],
            disabledTreeList: [] // 禁用的tree列表
        }
    },
    methods: {
        decreseStep() {
            if (this.activeStep === 1) return
            this.activeStep--
        },
        increseStep() {
            if (this.activeStep === 3) return
            if (this.activeStep === 1) {
                // 当前是角色信息状态，进行表单校验
                this.$refs.roleForm.validate((valid) => {
                    if (valid) {
                        // 校验通过才能进入下一个状态
                        this.activeStep++
                    }
                })
            }
            else if (this.activeStep === 2) {
                // 当前是权限信息状态
                this.roleForm.perms = []
                this.$refs.tree.forEach(tree => {
                    this.roleForm.perms.push(tree.getCheckedKeys())
                })
                // 如果长度为零 没有选中任何东西
                if (this.roleForm.perms.flat().length === 0) {
                    this.$message({
                        type: 'error',
                        message: '请至少选择一个权限点'
                    })
                } else {
                    // 如果长度不为零，进入到检查并完成
                    this.activeStep++
                    // 回填已选择数据
                    this.$refs.diabledTree.forEach((tree, index) => {
                        tree.setCheckedKeys(this.roleForm.perms[index])
                    })
                }
            }
        },
        // 获取功能权限列表
        async getTreeList() {
            const res = await getTreeListAPI()
            this.treeList = res.data
            this.disabledTreeList = JSON.parse(JSON.stringify(res.data))
            // 处理禁用
            this.addDisabled(this.disabledTreeList)
        },
        addDisabled(treeList) {
            treeList.forEach(item => {
                item.disabled = true
                // 递归处理
                if (item.children && item.children.length > 0) {
                    this.addDisabled(item.children)
                }
            })
        },
        async getRoleDetail() {
            const res = await getRoleDetailAPI(this.roleId)
            const { perms, remark, roleId, roleName } = res.data
            // 回填基础表单
            this.roleForm = { perms, remark, roleId, roleName }
            // 回填Tree
            this.$refs.tree.forEach((tree, index) => {
                tree.setCheckedKeys(this.roleForm.perms[index])
            })
        },
        async confirmAdd() {
            // 编辑
            if (this.roleId) {
                await updateRoleAPI(this.roleForm)
            } else {
                // 添加
                await createRoleUserAPI(
                    this.roleForm
                )
            }
            this.$message({
                type: 'success',
                message: `${this.$route.query.id ? '编辑' : '新增'}角色成功`
            })
            this.$router.back()
        }
    },
    mounted() {
        // 在初始化时候就加载好
        this.getTreeList()
        if (this.$route.query.id) {
            this.getRoleDetail()
        }
    },
    // 缓存roleId
    computed: {
        roleId() {
            return this.$route.query.id
        }
    }
}
</script>
<style scoped lang="scss">
.add-role {
    background-color: #f4f6f8;
    height: 100vh;
    font-size: 14px;

    .add-header {
        display: flex;
        align-items: center;
        padding: 0 20px;
        height: 64px;
        background-color: #fff;
        line-height: 64px;
        font-size: 16px;

        .left {
            span {
                margin-right: 4px;
            }

            .arrow {
                cursor: pointer;
            }
        }

        .right {
            text-align: right;
        }
    }

    .add-main {
        display: flex;
        background: #f4f6f8;

        .step-container {
            padding: 50px 20px 0;
            height: 200px;
            font-size: 14px;
        }

        .form-container {
            background-color: #fff;
            margin: 20px 200px 0;

            .title {
                height: 60px;
                line-height: 60px;
                padding-left: 20px;
            }

            .form {
                display: flex;
                justify-content: center;
                width: 700px;
                height: 230px;

                .el-form {
                    display: flex;
                    flex-wrap: wrap;
                    margin: 30px 0;

                    .el-form-item {
                        width: 100%;
                    }
                }
            }

            .tree-wrapper {
                display: flex;
                justify-content: space-between;
                height: 500px;
                width: 800px;

                .tree-item {
                    flex: 1;
                    width: 120px;
                    border-right: 1px solid #e4e7ec;
                    padding: 0px 4px;
                    text-align: center;

                    ::v-deep .el-tree-node {
                        white-space: normal; //关键代码!!!!!!!!!
                        padding: 0 0 6px;
                        text-align: left;

                        .el-tree-node__content {
                            height: 100%;
                            align-items: start;
                        }
                    }

                    .tree-title {
                        background: #f5f7fa;
                        text-align: center;
                        padding: 10px 0;
                        margin-bottom: 12px;

                    }
                }
            }

            .info {
                .form-item {
                    display: flex;
                    justify-content: left;
                    margin: 0 60px 10px;
                    .form-items1{
                        margin-right: 20px;
                    }
                }
                .form-items2{
                    margin:10px 60px;
                }
            }
        }
    }

    .add-footer {
        position: fixed;
        bottom: 0;
        width: 100%;
        padding: 24px 50px;
        color: #000000d9;
        font-size: 14px;
        background: #fff;
        text-align: center;
        //border-top: 1px solid #ccc;
    }
}
</style>